//
//  RTJBidIngRecordsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/10.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJBidIngRecordsController.h"
#import "RTJBidRecordsCell.h"
#import "RTJBidRecordsDetailsController.h"

@interface RTJBidIngRecordsController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property(nonatomic, assign) NSInteger currentPage;

@end

@implementation RTJBidIngRecordsController

-(NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    self.currentPage = 1;
    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
        
        self.currentPage = 1;
        [self loadData];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.currentPage ++;
        [self loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

-(void)loadData{
    
    
    [SVProgressHUD  showWithStatus:nil];
    
    if(self.data.count == 0){
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/lend" params:@{@"page":@(_currentPage),@"type":@"0"} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];

        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSArray *arr = [NSArray array];
                arr = data[@"data"][@"list"];
                
                if (arr.count > 0) {
                    
                    if (self.currentPage == 1) {
                        
                        [self.data removeAllObjects];
                    }
                    
                    [self.data addObjectsFromArray:arr];
                    
                    if (arr.count < ZXPAGE_SIZE) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    
                }else{
                    
                    if (self.currentPage == 1) {
                        
                        
                        [self.data removeAllObjects];
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                        [self.view showEmptyViewWithStr:@"您暂无相关出借数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                            [self loadData];
                        }];
                        
                    }else{
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                    }
                }
                    
                     [self.tableView reloadData];
                
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
            }
            
        }else{
            
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsCell";
    
    RTJBidRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
          
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsCell" owner:self options:nil][0];
        }

    cell.titleLab.text = _data[indexPath.row][@"name"];
    cell.typeLab.text = _data[indexPath.row][@"deal_status_format"];
    cell.dealTime.text = _data[indexPath.row][@"create_time_format"];
    cell.moenyLab.text = [NSString stringWithFormat:@"%@ 元",_data[indexPath.row][@"money_format"]];
    cell.shouyiLab.text = [NSString stringWithFormat:@"%@ 元",_data[indexPath.row][@"profit"]];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    RTJBidRecordsDetailsController *vc = [[RTJBidRecordsDetailsController alloc]init];
    vc.title = _data[indexPath.row][@"name"];
    vc.binId = [NSString stringWithFormat:@"%@",_data[indexPath.row][@"id"]];
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
